/**
 * @file    common_button.h
 * @author  yhy
 * @brief   按键组件
 * @version 1.0
 * @date    2025-07-09
 *
 * @copyright Copyright (c) 2025
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-07-02     yhy          the first version V1.00
 *
 */
#ifndef __COMMON_BUTTON_H__
#define __COMMON_BUTTON_H__

#include "common_config.h"

#include <stdbool.h>
#include <stdint.h>

/* 用于解决前向声明问题 */
typedef struct button_s button_t;

/* 回调函数类型 */
typedef void (*button_callback_f)(button_t *button);

/* 按键ID */
typedef enum
{
    BUTTON_ID_0  = 0U,
    BUTTON_ID_1  = 1U,
    BUTTON_ID_2  = 2U,
    BUTTON_ID_3  = 3U,
    BUTTON_ID_4  = 4U,
    BUTTON_ID_5  = 5U,
    BUTTON_ID_6  = 6U,
    BUTTON_ID_7  = 7U,
    BUTTON_ID_8  = 8U,
    BUTTON_ID_9  = 9U,
    BUTTON_ID_10 = 10U,
    BUTTON_ID_11 = 11U,
    BUTTON_ID_12 = 12U,
    BUTTON_ID_MAX
} button_id_e;

/* 按键事件 */
typedef enum
{
    BUTTON_EVENT_PRESS_DOWN = 0,   /*<! 按键按下  */
    BUTTON_EVENT_PRESS_UP,         /*<! 按键弹起  */
    BUTTON_EVENT_PRESS_REPEAT,     /*<! 按键重复  */
    BUTTON_EVENT_SINGLE_CLICK,     /*<! 按键单击  */
    BUTTON_EVENT_DOUBLE_CLICK,     /*<! 按键双击  */
    BUTTON_EVENT_LONG_PRESS_START, /*<! 按键长按  */
    BUTTON_EVENT_LONG_PRESS_HOLD,  /*<! 按键长按HOLD  */
    BUTTON_EVENT_MAX,              /*<! 按键事件数量  */
    BUTTON_EVENT_NONE_PRESS        /*<! 按键无按下  */
} button_event_e;

/* 按键结构体, button_s用于解决向前声明问题 */
typedef struct button_s
{
    button_id_e id;                                    /*<! 按键ID  */
    uint8_t     active_level;                          /*<! 按键按下的电平  */
    uint8_t (*get_gpio_level)(uint8_t btn_id);         /*<! 按键电平获取函数  */
    button_callback_f callback_list[BUTTON_EVENT_MAX]; /*<! 按键事件回调函数列表  */
} button_t;

result_e common_button_init(const button_t *button);
result_e common_button_register_callback(button_t *button, button_event_e event,
                                         button_callback_f callback);
void     common_button_scan(void);

#endif
